package com.itheima.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockk implements Runnable {
    private int tickets = 100;
    private int i;
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        try {
            while (true) {
                lock.lock();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + "出售第" + i + "张票");
                    i--;
                    tickets--;
                }
            }
        } finally {
            lock.unlock();
        }
    }
}
class LockTest{
    public static void main(String[] args) {
        ReentrantLockk rl=new ReentrantLockk();
        Thread td1=new Thread(rl,"一号窗口");
        Thread td2=new Thread(rl,"二号窗口");
        Thread td3=new Thread(rl,"三号窗口");
        td1.start();
        td2.start();
        td3.start();
    }
}
